/**
 * futures : masterlink.futures.vo.GroupId.java
 */
package masterlink.futures.vo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import masterlink.futures.util.WebUtils;

import baytony.util.Util;

/**
 * TODO Write the description
 *
 * @author : tonyyang
 * Create date : 2011/10/25
 * @version 1.0.0	
 */
@Entity
@Table(name="SYS_GROUP")
public class Group implements Serializable{

	private static final long serialVersionUID = -6866940421342134295L;
	@Id 
	@GeneratedValue(strategy=GenerationType.AUTO, generator="group_id") 
    @SequenceGenerator(name="group_id", sequenceName = "SYS_GROUP_ID")
	private Long id;
	@Column(name="name")
	private String name;
	@Column(name="remark")
	private String remark;
	@ManyToOne(fetch=FetchType.EAGER,cascade=CascadeType.MERGE)
	@JoinColumn(name="parent")
	private Group parent;
	@Column(name="system")
	private String system="N";
	@Column(name="dealer")
	private String dealer="N";
	@Column(name="SEQ")
	private Long seq=1l;
	@ManyToMany(fetch=FetchType.EAGER,cascade=CascadeType.MERGE)
	@JoinTable(name="GREEKS_LIMIT", joinColumns={@JoinColumn(name="ID")},
			inverseJoinColumns={@JoinColumn(name="ACCOUNT"),@JoinColumn(name="BROKER")})
	private List<Account> accounts=new ArrayList<Account>();
	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the remark
	 */
	public String getRemark() {
		return remark;
	}
	/**
	 * @param remark the remark to set
	 */
	public void setRemark(String remark) {
		this.remark = remark;
	}
	/**
	 * @return the parent
	 */
	public Group getParent() {
		return parent;
	}
	/**
	 * @param parent the parent to set
	 */
	public void setParent(Group parent) {
		this.parent = parent;
	}
	/**
	 * @return the system
	 */
	public String getSystem() {
		return system;
	}
	/**
	 * @param system the system to set
	 */
	public void setSystem(String system) {
		this.system = system;
	}
	/**
	 * @return the dealer
	 */
	public String getDealer() {
		return dealer;
	}
	/**
	 * @param dealer the dealer to set
	 */
	public void setDealer(String dealer) {
		if(!Util.isEmptyString(dealer)){
			this.dealer = dealer;
		}
	}
	/**
	 * @return the accounts
	 */
	public List<Account> getAccounts() {
		return accounts;
	}
	/**
	 * @param accounts the accounts to set
	 */
	public void setAccounts(List<Account> accounts) {
		this.accounts = accounts;
	}
	/**
	 * @return the seq
	 */
	public Long getSeq() {
		return seq;
	}
	/**
	 * @param seq the seq to set
	 */
	public void setSeq(Long seq) {
		this.seq = seq;
	}
	
	public boolean isSystem(){
		return Util.parseBoolean(system);
	}
	
	public boolean isDealer(){
		return Util.parseBoolean(dealer);
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Group))
			return false;
		Group other = (Group) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("Group [id=");
		builder.append(id);
		builder.append(", name=");
		if(!Util.isEmptyString(name)){
			builder.append(WebUtils.ascii2big5(name));
		}
		builder.append(", seq=");
		builder.append(seq);
		builder.append(", remark=");
		builder.append(remark);
		builder.append(", parent=");
		builder.append(parent);
		builder.append(", system=");
		builder.append(system);
		builder.append(", dealer=");
		builder.append(dealer);
		builder.append(", accounts=");
		builder.append((accounts!=null?accounts.size():null));
		builder.append("]");
		return builder.toString();
	}
	
}
